package com.qyhl.webtv.module_circle.circle.msg.notice;


import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleNoticeBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by helin on 2018/1/30.
 */

public class NoticeModel implements NoticeContract.NoticeModel {


    private final NoticeContract.NoticePresenter presenter;

    NoticeModel(NoticeContract.NoticePresenter presenter) {
        this.presenter = presenter;
    }


    @Override
    public void getNoticeData(int lastMsgId, final int type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=systemMsg" + "&username=" + userName + "&lastMsgId=" + lastMsgId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleNoticeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.getMessageError();
                    }

                    @Override
                    public void onSuccess(List<CircleNoticeBean> circleNoticeBeans) {
                        presenter.getMessageSuccess(circleNoticeBeans, type);
                    }
                });


//        OkHttpUtils.post()
//                .url(Url.CIRCLE_URL)
//                .addParams("method", "systemMsg")
//                .addParams("siteId", MyConfig.SITE_ID + "")
////                .addParams("username",  MyConfig.phoneNum + "")
//                .addParams("username", MyConfig.userName)
//                .addParams("lastMsgId", lastMsgId + "")
//                .build()
//                .execute(new GenericsCallback<JsonCircleNotice>(new JsonGenericsSerializator()) {
//
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        if (call.isCanceled()) {
//                            presenter.getMessageError();
//                        }
//                    }
//
//                    @Override
//                    public void onResponse(JsonCircleNotice response, int id) {
//                        if (response.getCode() == 200) {
//                            presenter.getMessageSuccess(response.getData(), type);
//                        } else {
//                            presenter.getMessageError();
//                        }
//                    }
//                });


    }


}
